<?php

class teammembers
{
	var $mess;
	var $teamid;
	var $toRender = Array();

	function teammembers($teamid)
	{
		$mess = '';
		$this->teamid = $teamid;
		if(isset($_POST['removeRecord']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']))
		{
			$this->doEdit();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		$qUpdateChamps = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
		$result = $db->sql_query($qUpdateChamps);
		if($row = $db->sql_fetchrow($result))
		{
			//get the record
			$qDel = "delete from ". TEAMMEMBERS_TABLE . " Where tm_team = " . $this->teamid . " AND tm_driver = " . $_POST['driver'];
			$db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". TEAMMEMBERS_TABLE . " values(" .
							 $_POST['driver'] . ", " . $this->teamid . ", " . $_POST['admin']
							 . ", " . $_POST['approved']  .')';
			$qSel = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
			$result = $db->sql_query($qSel);
			if($row = $db->sql_fetchrow($result))
			{
				$db->sql_query($qUpd);
				$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qSel =  'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
			$result = $db->sql_query($qSel);
			if($row = $db->sql_fetchrow($result))
			{
				$qUpd = "update  ". TEAMMEMBERS_TABLE . " set tm_approved = " . $_POST['approved'] .
													  ", tm_admin = " . $_POST['admin'] .
													  " WHERE tm_driver = " . $_POST['driver'] . ' AND ' .
													  " tm_team = " . $this->teamid;
				$db->sql_query($qUpd);
				$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		global $db;
		
		$valid = true;
		
		if($checkID)
		{
			$valid = isset($_POST['driver']) && is_numeric($_POST['driver']);
		}
		
		$_POST['admin'] = isset($_POST['admin']) ? $_POST['admin'] : 0;
		$_POST['approved'] = isset($_POST['approved']) ? $_POST['approved'] : 0;
		
		$_POST['admin'] = $_POST['admin'] == 1 ? 1 : 0;
		$_POST['approved'] = $_POST['approved'] == 1 ? 1 : 0;
		
		return $valid;	  
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_teammembers.php');
		
		//get the record
		
		$q = "SELECT *, pf_drivername as user_drivername 
			  FROM " . TEAMMEMBERS_TABLE . ", " . USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d 
			  WHERE tm_driver = u.user_id AND u.user_id = d.user_id AND tm_team = " . $this->teamid . 
				" ORDER BY pf_drivername ASC";
		$result = $db->sql_query($q);
		$introMess = false;
		
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$introMess = true;
			}
			
			$this->toRender[] = new edit_teammembers(false, $this->teamid, $row);
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_teammembers.php');
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new edit_teammembers(true, $this->teamid);
		
		
	}

	
}
?>